package com.zou.recruit.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.zou.recruit.entity.User;
import com.zou.recruit.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author: 邹祥发
 * @date: 2021/9/22 10:43
 */
@Controller
public class RegisterController {

    @Resource
    private UserMapper userMapper;

    //注册
    @RequestMapping(value = "check_register", method = RequestMethod.POST)
    public String register(HttpServletRequest request, Model model, HttpSession session) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String rePassword = request.getParameter("rePassword");
        if (userMapper.selectName(username)!=null){
            //用户名存在
            model.addAttribute("msg", "用户名已存在！");
            return "user/register";
        }else {
            //用户名不存在
            if (password.equals(rePassword)){
                //密码相同
                userMapper.registerUser(username,password);

                session.setAttribute("user",username);
                return "index";
            }else {
                //密码不同
                model.addAttribute("msg", "密码不一致！");
                return "user/register";
            }
        }
    }
}
